<template>
  <div class="alloc-menu">
    <el-tree
      :data="nodeList"
      show-checkbox
      node-key="id"
      ref="tree"
      highlight-current
      :default-checked-keys="checkedKeys"
      :props="defaultProps">
    </el-tree>
    <div class="btns-line">
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="resetChecked">清空</el-button>
    </div>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAllCategory, getAllResource, getRoleResource, allocateRoleResources } from '@/services/resource'
import { Tree } from 'element-ui'

export default Vue.extend({
  name: 'AllocResource',

  props: {
    roleId: {
      type: [String, Number],
      required: true
    }
  },

  data () {
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      allCategory: [],
      allResource: [],
      checkedKeys: []
    }
  },

  computed: {
    nodeList () {
      if (!this.allCategory.length || !this.allResource.length) return []
      const result: any = []
      this.allCategory.forEach((category: any) => {
        result.push({
          id: category.id + 'category',
          name: category.name,
          children: this.allResource.filter((i: any) => {
            return i.categoryId === category.id
          })
        })
      })
      return result
    }
  },

  mounted () {
    this.loadAllCategory()
    this.loadAllResource()
    this.loadRoleResource()
  },

  methods: {
    loadAllCategory () {
      getAllCategory().then(res => {
        if (res.data.code === '000000') {
          this.allCategory = res.data.data
        }
      })
    },

    loadAllResource () {
      getAllResource().then(res => {
        if (res.data.code === '000000') {
          this.allResource = res.data.data
        }
      })
    },

    loadRoleResource () {
      getRoleResource(this.roleId).then(res => {
        if (res.data.code === '000000') {
          const list = res.data.data
          const tmp: [] = []
          this.getKeys(list, tmp)
          this.checkedKeys = tmp
        }
      })
    },

    getKeys (list: any[], ret: any[]) {
      list.forEach(item => {
        if (!item.selected) return
        item.categoryId && ret.push(item.id)
        if (item.resourceList) {
          this.getKeys(item.resourceList, ret)
        }
      })
    },

    resetChecked () {
      (this.$refs.tree as Tree).setCheckedKeys([])
    },

    save () {
      let resourceIdList = (this.$refs.tree as Tree).getCheckedKeys()
      resourceIdList = resourceIdList.filter((i: any) => {
        return typeof i === 'number'
      })
      allocateRoleResources({
        roleId: this.roleId,
        resourceIdList
      }).then(res => {
        if (res.data.code === '000000') {
          this.$message.success('操作成功')
          this.$router.back()
        }
      })
    }
  }
})
</script>

<style lang="scss" scoped>
.alloc-menu{
    background: #fff;
    padding: 20px;
}
.btns-line{
    margin-top: 20px;
    text-align: center;
}
</style>
